using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;

public class TurretData
{
    public int id; //ID
    public string name; //名字
    public string type; //类型
    public int level; //等级
    public int atkPower; //攻击力
    public float atkInterval; //攻击间隔
    public float atkDis; //攻击范围
    public int upLevelGold; //升级花费的金币（-1代表已经满级）
    public int saleGole; //出售获得的金币
    public string buildSale; //可以建造的图片替换
    public string cantBuildSale; //不可以建造的图片替换
    public string bgIcon; //每次升级后的背景图片替换
    public string turretIcon; //炮塔每次升级后的图片替换
    public string buttlePath; //子弹的路径
    public string buttleIconPath; //子弹ICON图片替换
    public string dicIcon; //距离显示ICON
    public string des; //描述

    public string test1;
    public string test2;
    public string test3;
}

public class Holder
{
    public int x;
    public int y;
}

public class MonsterRounds
{
    public int monster;
    public int count;
}

public class LevelData
{
    public string name;
    public string cardImage;
    public string backGround;
    public string road;
    public int initScore;
    public List<Holder> holders;
    public List<Holder> paths;
    public MonsterRounds monsterRounds;
}


public class XmlParse : MonoBehaviour
{
    XmlDocument xmlDoc;

    List<TurretData> turretDatas = new List<TurretData>();

    void Start()
    {
        
        //解析炮塔数据
        //Parse_turret();
        //解析关卡数据
        Parse_level();
        
    }

    public LevelData Parse_level()
    {
        xmlDoc = new XmlDocument();
        
        xmlDoc.Load(Application.dataPath+"/Resources/Res/Levels/BigLevel1/level0.xml");
        XmlNode xmlName = xmlDoc.SelectSingleNode("//Name");
        XmlNode xmlCardImage = xmlDoc.SelectSingleNode("//CardImage");
        XmlNode xmlBackGround = xmlDoc.SelectSingleNode("//Background");
        XmlNode xmlRoad = xmlDoc.SelectSingleNode("//Road");
        XmlNode xmlInitScore = xmlDoc.SelectSingleNode("//InitScore");
        List<Holder> holders = GetLevelPoint(xmlDoc, "//Holder/Point");
        List<Holder> paths = GetLevelPoint(xmlDoc, "//Path/Point");
        MonsterRounds monsterRounds= GetMonsterRounds(xmlDoc,"//Rounds/Round");

        LevelData levelData = new LevelData()
        {
            name = xmlName.InnerText,
            cardImage = xmlCardImage.InnerText,
            backGround = xmlBackGround.InnerText,
            road = xmlRoad.InnerText,
            initScore =int.Parse(xmlInitScore.InnerText),
            holders = holders,
            paths = paths,
            monsterRounds = monsterRounds
        };
        
        return levelData;
    }


    public void Parse_turret()
    {
        xmlDoc = new XmlDocument();
            
        xmlDoc.Load(Application.dataPath + "/Resources/XML/paota1.xml");
        XmlNodeList rowList = xmlDoc.SelectNodes("//row");
        bool isHeaderRow = true;
        string[] headers = null;
        int index = 0;
        
        foreach (XmlNode item in rowList)
        {
            //XmlNodeList cellList = item.SelectNodes("*");
            if (isHeaderRow)
            {
                headers = GetColumnValues(item,"*");
                isHeaderRow = false;
                index++;
                continue;
            }

            if (index==1)
            {
                index++;
                continue;
            }

            TurretData data = new TurretData();
            string[] columnValues = GetColumnValues(item,"*");
            for (int i = 0; i < headers.Length && i < columnValues.Length ; i++)
            {
                switch (headers[i])
                {
                    case "ID": 
                        data.id = int.Parse(columnValues[i]);
                        break;
                    case "NAME":
                        data.name = columnValues[i];
                        break;
                    case "TYPE":
                        data.type = columnValues[i];
                        break;
                    case "LEVEL":
                        data.level = int.Parse(columnValues[i]);
                        break;
                    case "ATKPOWER":
                        data.atkPower = int.Parse(columnValues[i]);
                        break;
                    case "ATKINTERVAL":
                        data.atkInterval = float.Parse(columnValues[i]);
                        break;
                    case "ATKDIS":
                        data.atkDis = float.Parse(columnValues[i]);
                        break;
                    case "UPLEVELGOLD":
                        data.upLevelGold = int.Parse(columnValues[i]);
                        break;
                    case "SALEGGOLD":
                        data.saleGole = int.Parse(columnValues[i]);
                        break;
                    case "BUILDSALE":
                        data.buttlePath = columnValues[i];
                        break;
                    case "CANTBUILDSALE":
                        data.cantBuildSale = columnValues[i];
                        break;
                    case "BGICON":
                        data.bgIcon = columnValues[i];
                        break;
                    case "TURRETICON":
                        data.turretIcon = columnValues[i];
                        break;
                    case "BUTTLEPATH":
                        data.buttlePath = columnValues[i];
                        break;
                    case "BUTTLEICONPATH":
                        data.buttleIconPath = columnValues[i];
                        break;
                    case "DICICON":
                        data.dicIcon = columnValues[i];
                        break;
                    case "DES":
                        data.des = columnValues[i];
                        break;
                }
            }
            turretDatas.Add(data);
        }
    }


    public MonsterRounds GetMonsterRounds(XmlNode xmlNode,string columnName)
    {
        MonsterRounds monsterRounds=new MonsterRounds();
        XmlNode monsterNode = xmlNode.SelectSingleNode(columnName);

        if (monsterNode.Attributes!=null && monsterNode.Attributes["Monster"]!=null && monsterNode.Attributes["Count"]!=null
            && int.TryParse(monsterNode.Attributes["Monster"].Value,out int monster) && int.TryParse(monsterNode.Attributes["Count"].Value,out int count))
        {
            monsterRounds = new MonsterRounds() {monster = monster, count = count};
        }

        return monsterRounds;
    }
    
    static List<Holder> GetLevelPoint(XmlNode xmlNode,string columnName)
    {
        List<Holder> holders = new List<Holder>();
        XmlNodeList pointList = xmlNode.SelectNodes(columnName);
        foreach (XmlNode pointNode in pointList)
        {
            if (pointNode.Attributes!=null && pointNode.Attributes["X"]!=null && pointNode.Attributes["Y"]!=null
                && int.TryParse(pointNode.Attributes["X"].Value,out int x) && int.TryParse(pointNode.Attributes["Y"].Value,out int y))
            {
                holders.Add(new Holder(){x =x,y=y});
            }
        }
        
        return holders;
    }

    static string[] GetColumnValues(XmlNode rowNode,string columnName)
    {
        List<string> values = new List<string>();
        XmlNodeList colNodes = rowNode.SelectNodes(columnName);
        foreach (XmlNode col in colNodes)
        {
            values.Add(col.InnerText);
        }
        return values.ToArray();
    }

    void Update()
    {
    }
}